/**
 * @author Pieter Michels / wellconsidered
 * pieter@wellconsidered.be
 *
 * Open source under the GNU Lesser General Public License (http://www.opensource.org/licenses/lgpl-license.php)
 * Copyright © 2009 Pieter Michels / wellconsidered
 * 
 * This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License 
 * as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty 
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License along with this library; 
 * if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 */
 
package be.wellconsidered.apis.lighthouse.resources
{
	import be.wellconsidered.apis.lighthouse.LightHouse;
	import be.wellconsidered.apis.lighthouse.connectors.*;
	import be.wellconsidered.apis.lighthouse.connectors.events.*;
	import be.wellconsidered.apis.lighthouse.resources.events.MessageEvents;
	import be.wellconsidered.logging.Logger;
	
	public class Messages extends AbstractResource
	{		
		private var _xmlNewMessage:XML;
		
		public function Messages(lighthouseReference:LightHouse)
		{
			super(lighthouseReference);
		}
		
		/**
		 * List Messages
		 */
		public function listMessages(projectID:int):void
		{
			Logger.log("List Messages");
			
			var connGET:GET = new GET(_lhRef.getURL() + "projects/" + projectID + "/messages.xml", _lhRef.getToken());
			
			connGET.addEventListener(GETEvents.COMPLETE, onMessageListingComplete, false, 0, true);
			
			function onMessageListingComplete(e:GETEvents):void
			{
				dispatchEvent(new MessageEvents(MessageEvents.LIST_COMPLETE, false, false, new XML(e.data)));	
			}
		}
		
		/**
		 * Get Message
		 */
		public function getMessage(projectID:int, messageID:int):void
		{
			Logger.log("Get Message (" + ticketID + ")");
			
			var connGET:GET = new GET(_lhRef.getURL() + "projects/" + projectID + "/messages/" + messageID + ".xml", _lhRef.getToken());
			
			connGET.addEventListener(GETEvents.COMPLETE, onMessageFetchComplete, false, 0, true);
			
			function onMessageFetchComplete(e:GETEvents):void
			{
				dispatchEvent(new MessageEvents(MessageEvents.GET_COMPLETE, false, false, new XML(e.data)));	
			}
		}
		
		/**
		 * New Message
		 */
		public function newMessage(projectID:int):void
		{
			Logger.log("New Message");
			
			var connGET:GET = new GET(_lhRef.getURL() + "projects/" + projectID + "/messages/new.xml", _lhRef.getToken());
			
			connGET.addEventListener(GETEvents.COMPLETE, onNewMessageDataComplete, false, 0, true);
			
			function onNewMessageDataComplete(e:GETEvents):void
			{
				_xmlNewMessage = new XML(e.data);
				
				dispatchEvent(new MessageEvents(MessageEvents.NEW_COMPLETE, false, false, _xmlNewMessage));	
			}
		}
		
		/**
		 * Create Message
		 */
		public function createMessage(projectID:Number, messageTitle:String, messageBody:String):void
		{
			Logger.log("Create Message");
			
			var xmlMessage:XML = _xmlNewMessage;
			
			xmlMessage..title = messageTitle;
			xmlMessage..body = messageBody;
			
			trace(xmlMessage);
					
			var connPOST:POST = new POST(_lhRef.getURL() + "projects/" + projectID + "/messages.xml", _lhRef.getToken(), _lhRef.getBase64Encoding(), xmlMessage);
			
			connPOST.addEventListener(POSTEvents.COMPLETE, onCreateMessageComplete, false, 0, true);
			
			function onCreateMessageComplete(e:POSTEvents):void
			{				
				dispatchEvent(new MessageEvents(MessageEvents.CREATE_COMPLETE, false, false, new XML(e.data)));	
			}
		}
		
		/**
		 * Create Comment
		 */
		public function createComment(projectID:Number, commentTitle:String, commentBody:String):void
		{
			Logger.log("Create Message");
			
			var xmlComment:XML = _xmlNewMessage;
			
			xmlComment..title = commentTitle;
			xmlComment..body = commentBody;
			
			trace(xmlComment);
					
			var connPOST:POST = new POST(_lhRef.getURL() + "projects/" + projectID + "/messages/5/comments.xml", _lhRef.getToken(), _lhRef.getBase64Encoding(), xmlComment);
			
			connPOST.addEventListener(POSTEvents.COMPLETE, onCreateCommentComplete, false, 0, true);
			
			function onCreateCommentComplete(e:POSTEvents):void
			{				
				dispatchEvent(new MessageEvents(MessageEvents.COMMENT_ADDED, false, false, new XML(e.data)));	
			}
		}
		
		/**
		 * Update Message
		 */
		public function updateMessage(projectID:int, messageXML:XML):void
		{
			Logger.log("Update Message (" + messageXML.number + ")");
			
			var connPUT:PUT = new PUT(_lhRef.getURL() + "projects/" + projectID + "/messages/" + messageXML.number + ".xml", _lhRef.getToken(), _lhRef.getBase64Encoding(), messageXML);
			
			connPUT.addEventListener(PUTEvents.COMPLETE, onUpdateMessageComplete, false, 0, true);
			
			function onUpdateMessageComplete(e:PUTEvents):void
			{				
				dispatchEvent(new MessageEvents(MessageEvents.UPDATE_COMPLETE, false, false, new XML(e.data)));	
			}
		}
		
		/**
		 * Delete Message
		 */
		public function deleteMessage(projectID:int, messageID:int):void
		{
			Logger.log("Delete Message (" + messageID + ")");
			
			var connDELETE:DELETE = new DELETE(_lhRef.getURL() + "projects/" + projectID + "/messages/" + messageID + ".xml", _lhRef.getToken(), _lhRef.getBase64Encoding());
			
			connDELETE.addEventListener(DELETEEvents.COMPLETE, onDeleteMessageComplete, false, 0, true);
			
			function onDeleteMessageComplete(e:DELETEEvents):void
			{				
				dispatchEvent(new MessageEvents(MessageEvents.DELETE_COMPLETE, false, false, e.data));	
			}
		}
	}
}